package com.lxh.updateprefixcode;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.lxh.model.ContactInfors;
import com.lxh.model.PhoneNumber;
import com.lxh.model.Utils;
import com.lxh.viewadaptor.CustomListAdapter;

public class CustomUpdateActivity extends Activity implements View.OnDragListener, View.OnClickListener{
	
	private ListView listContactsView;
	private CustomListAdapter customListAdaptor;
	private ContentResolver contentResolver;
	private ArrayList<ContactInfors> listContacts;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.activity_manual_update);

		this.listContactsView = (ListView) findViewById(R.id.listContacts);
		contentResolver = getContentResolver();
		
		initialView();
	}
	
	private void initialView() {
		
		this.listContacts = this.getContactInfors();
		this.showListContacts(this.listContacts);
	}
	
	public ArrayList<ContactInfors> getContactInfors(){
		ArrayList<ContactInfors> results = new ArrayList<ContactInfors>();
		
		HashMap<String,String> prefixCodePairs = Utils.getPrefixCodePairs();
		
		ArrayList<String> allContactIds = Utils.fetchContacts(this.contentResolver);
		ContactInfors contactInfor;
		ArrayList<String> contactPhoneNumbers;
		
		for(String contactId: allContactIds){

			contactPhoneNumbers = Utils.retrieveContactNumbers(this.contentResolver, contactId);

			if(contactPhoneNumbers.size()>0){
				
				contactInfor = new ContactInfors(prefixCodePairs, contactId, contactPhoneNumbers);
				results.add(contactInfor);
			}
		}
		
		return results;
	}

	public void showListContacts(ArrayList<ContactInfors> listContacts){

		customListAdaptor = new CustomListAdapter(this);
		customListAdaptor.setListItems(listContacts);
		listContactsView.setAdapter(customListAdaptor);
		listContactsView.setOnDragListener(this);
		
	}
	
	private void processUpdatePhoneNumber(ArrayList<ContactInfors> listContacts, int position){
		
		ContactInfors submitedContact = listContacts.get(position);
		
		ArrayList<PhoneNumber> phoneNumbers = submitedContact.getPhoneNumbers();
		boolean isSaved = false;
				
		//------------ Update Phone Number to DB ------//
		for(PhoneNumber phoneNumber: phoneNumbers){
			isSaved = Utils.updateContactNumber(this.contentResolver, submitedContact.getContactId(), phoneNumber.getNewFullNumber(), phoneNumber.getOldFullNumber());
		}
		
		//------------- Update View -----------//
		if(isSaved==true){
			listContacts.remove(position);
			this.listContactsView.removeViewAt(position);
		}
		
		Log.e("UpdatePrefixCode", "Save result: " + isSaved);
	}

	@Override
	public boolean onDrag(View v, DragEvent event) {
		
		final int action = event.getAction();
		
		switch(action){
			case DragEvent.ACTION_DRAG_STARTED:
			break;
			
			case DragEvent.ACTION_DRAG_ENDED:
			break;
		}
		
		return false;
	}

	@Override
	public void onClick(View v) {
		int selectedNumber = (Integer) v.getTag();
		Log.e("UpdatePrefixCode", "Tag: " + selectedNumber);
		this.processUpdatePhoneNumber(this.listContacts, selectedNumber);
	}
	
	
}	
